package recursive;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: liuchong
 * @create: 2023/2/19 22:05
 * @description: 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
 * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢？
 */
public class ClimbStairs_70 {
    private Map<Integer, Integer> map = new HashMap<>();
    public int climbStairs(int n) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        if (null != map.get(n)) {
            return map.get(n);
        }
        else {
            int result = climbStairs(n - 1) + climbStairs(n - 2);
            map.put(n, result);
            return result;
        }
    }
    public int climbStairsLoop(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        int result = 0;
        int pre = 2;
        int prePre = 1;
        for (int i = 3; i <= n; i++) {
            result = pre + prePre;
            prePre = pre;
            pre = result;
        }
        return result;
    }
    public static void main(String[] args) {
        System.out.println(new ClimbStairs_70().climbStairsLoop(6));
    }
}
